// pages/shift/shift.js
// import { ShiftModel } from '../../models/shift.js'
// import { OverTimeModel } from '../../models/overtime.js'
import { ShowDay } from '../../lib/enum.js'
import { config } from '../../config.js'
// import { Weather } from '../../models/weather.js'
// import { ToDoListModel } from '../../models/todolist.js'
import { XinHua } from '../../models/xinhua.js'
import { JinShan } from '../../models/jinshan.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog.js'
// const shift = new ShiftModel()
// const overtime = new OverTimeModel()
// const weather = new Weather()
// const todolistModel = new ToDoListModel()
const xinhuaModel = new XinHua()
const jinshanModel = new JinShan()
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: config.image_urls,
    todayShift:null,
    weekShifts:null,
    overtimes:null,
    backColor:"black",
    arrowColor:"gray",
    today_overtime:'0',
    statusBarHeight: 0,
    titleBarHeight: 0,
    swiperHeight: 0,
    userInfo: null,
    today_date:null,
    show: false,
    showpopup: false,
    createShiftValue: null,
    weather_data: null,
    xinhuaValue: "",
    xinhuaData: null,
    xinhuaType: 0,
    noxinhuaValue: "",
    jinshanData: [],
    // columns: [
    //   {
    //     values: config.shiftItems.shift_types,
    //     className: 'column1',
    //     defaultIndex: 0
    //   },
    //   {
    //     values: config.shiftItems.start_hour,
    //     className: 'column2',
    //     defaultIndex: 8
    //   },
    //   {
    //     values: config.shiftItems.start_minute,
    //     className: 'column3',
    //   },
    //   {
    //     values: config.shiftItems.end_hour,
    //     className: 'column4',
    //     defaultIndex: 17
    //   },
    //   {
    //     values: config.shiftItems.end_minute,
    //     className: 'column5',
    //     defaultIndex: 0
    //   },
    //   {
    //     values: config.shiftItems.over_times,
    //     className: 'column6',
    //   }
    // ]
  },

  // 授权登录
  onLogin: function () {
    wx.getSetting({
      success: (res)=>{
        if (res.authSetting['scope.userInfo']) {
          // token.verify()
        } else {
          Dialog.confirm({
            title: '用户授权',
            message: '小程序需要使用您的信息，请确认，谢谢！',
          })
            .then(() => {
              wx.navigateTo({
                url: '/pages/login/index'          
              })
            })
            .catch(() => {
              // on cancel
            })
        }
      }
    })
  },
 

  // 日历界面的显示影藏
  // onDisplay: function() {this.setData({show: true})},
  // onClose() {this.setData({show: false})},
  // formatDate(date) {
  //   date = new Date(date)
  //   let td = date.getMonth() + 1 +'/' + date.getDate()
  //   let tw = ShowDay[(date.getDay())]
  //   let todayDate = td + ' '+ tw
  //   return todayDate
  // },
  // async onConfirm(event) {
  //   this.setData({
  //     show: false,
  //     today_date: this.formatDate(event.detail)
  //   })
  //   let uid = wx.getStorageSync('uid') 
  //   let now = new Date()
  //   let day = String(now.getFullYear()) +'/'+
  //    this.data.today_date.substring(0,4)    
  //   const ts = await shift.getTodayClass(uid, day)    
  //   this.setData({
  //     todayShift: ts.result || '',
  //   })
  //   // this.methods._checkTodayShift(this.data.todayShift)
  //   const ot = await overtime.getToday(uid)
  //   this.setData({
  //     today_overtime: ot.result.over_time || '0'
  //   })
  // },

  // 弹出层，构建班次创建的页面
  // toCreateShiftPage() {this.setData({showpopup:true})},
  // onPopClose() {this.setData({showpopup: false})},
  // onPickerCancel() {this.setData({showpopup: false})},
  // onPickerChange (e) {
  //   // console.log(e.detail)
  //   const createShiftValue = e.detail.value
  //   console.log(createShiftValue)
  //   this.setData({
  //     createShiftValue: createShiftValue
  //   })
  // },
  // async onPickerConfirm () {
  //   let that = this
  //   const wuid = wx.getStorageSync('uid')
  //   const shiftValue = this.data.createShiftValue
  //   console.log(shiftValue)
  //   const ot = this.data.createShiftValue[5]
  //   let now = new Date()
  //   const date = String(now.getFullYear()) +'/'+ this.data.today_date.substring(0,4)
  //   const addOneShiftResult = await shift.addOneShift(wuid, shiftValue, date)
  //   const addOneOverTimeResult = await overtime.addOneOverTime(wuid, ot, date)
  //   if (addOneShiftResult.result == 'fail' ) {
  //     const updateOneShiftRes = await shift.updateOneShift(wuid, shiftValue, date)
  //     console.log(updateOneShiftRes)
  //   }
  //   if (addOneOverTimeResult.result == 'fail') {
  //     const updateOneOvertimeRes = await overtime.updateOneOverTime(wuid, ot, date)
  //     console.log(updateOneOvertimeRes)
  //   }
  //   console.log(addOneShiftResult)
  //   console.log(addOneOverTimeResult)
    // const day = String(now.getFullYear()) + this.data.today_date.substring(0,4)
  //   setTimeout( async function () {
  //     const ts = await shift.getTodayClass(wuid, date)    
  //     that.setData({
  //       todayShift: ts.result || '',
  //     })
  //     const newOt = await overtime.getToday(wuid, date)
  //     that.setData({
  //       today_overtime: newOt.result.over_time || '0'
  //     })  
  //   }, 500)       
  //   this.setData({
  //     showpopup: false
  //   })
  // },

  
  // 字典层
  onGoXinHua () {
    wx.navigateTo({
      url: "/pages/xinhua/xinhua"
    })
  },
  onGoBaiduTrans () {
    wx.navigateTo({
      url: "/pages/baidutrans/baidutrans"
    })
  },
  onXinHuaChange (e) {
    this.setData({
      xinhuaValue: e.detail
    })
  },
  async onXinHuaSearch () {
    const word = this.data.xinhuaValue
    let result = await xinhuaModel.getData(word)
    console.log(result)
    if ( result.result == "fail") {
      this.setData({
        noxinhuaValue: "对不起，内容尚未收录！",
        xinhuaType: 1
      })
    } else {
      this.setData({
        xinhuaData: result.result,
        xinhuaType: result.result.type
      })
    }
  },

  async onXinHuaClick () {
    const word = this.data.xinhuaValue
    let result = await xinhuaModel.getData(word)
    console.log(result)
    if ( result.result == "fail") {
      this.setData({
        noxinhuaValue: "对不起，内容尚未收录！",
        xinhuaType: 1
      })
    } else {
      this.setData({
        xinhuaData: result.result,
        xinhuaType: result.result.type
      })
    }
  },

  //番茄时钟
  onGoTomato () {
    wx.navigateTo({
      url: "/pages/tomato/tomato"
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) { 
    // let userInfo = null
    // wx.getSetting({
    //   success: (res)=>{
    //     if (res.authSetting['scope.userInfo']) {
    //       wx.getUserInfo({
    //         lang: "zh_CN",
    //         success: (async res => {
    //           // console.log(res.userInfo)
    //           // let wuid = wx.getStorageSync('uid')
    //           // let city = res.userInfo.city
    //           // const now = new Date()
    //           // const day = String(now.getFullYear()+'/'+ (now.getMonth() + 1) + '/' + now.getDate())
    //           // const result = await weather.getTodayWeather(wuid, city, day)
    //           // // console.log(result)
    //           // this.setData({
    //           //   weather_data: result.result
    //           })
    //         })
    //       })
    //     }
    //   }
    // })    
    const winWidth = 750
    const swiperHeight = (winWidth - 40) * 540/1065
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight,
      titleBarHeight: app.globalData.titleBarHeight,
      swiperHeight: swiperHeight,
    })
    // let date = new Date()
    // let td = date.getMonth() + 1 +'/' + date.getDate()
    // let tw = ShowDay[(date.getDay())]
    // let todayDate = td + ' '+ tw
    // this.setData({
    //   today_date: todayDate
    // })
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  methods: {
    // _modifyWeekShifts: function (ws,ots) {
    //   let weekShift = {weekDate:'',week:'',shift_type:'',over_time:''}
    //   let weekShifts = []
    //   for (let i=0; i<7; i++){
    //     const dateFromServer = ws[i].date || '无'
    //     const shiftType = ws[i].shift_type
    //     const ot = ots[i]==null?'0':ots[i].over_time
    //     let date = new Date(dateFromServer)
    //     let weekDate = date.getMonth()+1+'月'+date.getDate()+'日'
    //     let week = ShowDay[date.getDay()]
    //     weekShift = {
    //       weekDate: weekDate,
    //       week:week,
    //       shift_type: shiftType,
    //       over_time: ot || '0'
    //     }
    //     weekShifts.push(weekShift)
      // }
      // return weekShifts
    // }
  },

  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {    
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      active: "home"
    })
  } 
    let res = await jinshanModel.getData()
    
    console.log(res)
    this.setData({
      jinshanData: res.result
    })
    // let uid = wx.getStorageSync('uid') 
    // let now = new Date()
    // let day = String(now.getFullYear()) +'/'+
    //  this.data.today_date.substring(0,4)
    // const ts = await shift.getTodayClass(uid, day)
    // this.setData({
    //   todayShift: ts.result || '',
    // })
    // const ot = await overtime.getToday(uid)
    // this.setData({
    //   today_overtime: ot.result.over_time || '0'
    // })      
    // let userInfo = null
    // wx.getSetting({
    //   success: (res)=>{
    //     if (res.authSetting['scope.userInfo']) {
    //       wx.getUserInfo({
    //         lang: "zh_CN",
    //         success: (async res => {
    //           // console.log(res.userInfo)
    //           let wuid = wx.getStorageSync('uid')
    //           let city = res.userInfo.city
    //           const now = new Date()
    //           const day = String(now.getFullYear()+'/'+ (now.getMonth() + 1) + '/' + now.getDate())
    //           const result = await weather.getTodayWeather(wuid, city, day)
    //           // console.log(result)
    //           this.setData({
    //             weather_data: result.result
    //           })
    //         })
    //       })
    //     }
    //   }
    // })    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})